package com.mobile.handler;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.mobile.common.base.handler.BaseHandler;
import com.mobile.common.base.hibernate.SimpleDao;
import com.mobile.common.util.StringUtil;
import com.mobile.persistent.dao.PurviewDao;
import com.mobile.persistent.dao.UserPurviewDao;
import com.mobile.persistent.entity.Purview;
import com.mobile.persistent.entity.UserPurview;

@Service
public class UserPurviewHandler extends BaseHandler<UserPurview, Long> {

	@Resource private UserPurviewDao userPurviewDao;
	@Resource private PurviewDao purviewDao;
	
	@Override
	public SimpleDao<UserPurview, Long> getEntityDao() {
		return userPurviewDao;
	}

	public List<UserPurview> getPurveiwByUid(Long uid){
		List<UserPurview> userPur = Lists.newArrayList();
		if(uid!=null){
			userPur = userPurviewDao.getPurviewsByUid(uid);
			for(UserPurview up:userPur){
				Long id = up.getPurview().getId();
				Purview pur = (Purview)purviewDao.get(id);
				System.out.println(pur.getOperCode());
			}
		}
		return userPur;
	}
	
	/**
	 * 获取操作权限码的列表(用逗号连接)
	 * @param uid 用户id
	 * @return 操作权限码的列表(用逗号连接)
	 */
	public List<String> getOperCode(Long uid){
		List<String> operCode = Lists.newArrayList();
		List<UserPurview> userPur = this.getPurveiwByUid(uid);
		if(!userPur.isEmpty()){
			for(UserPurview up:userPur){
				if(up.getPurview()!=null)
				operCode.add(up.getPurview().getOperCode());
			}
		}
		return operCode;
	}
}
